<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreatePayStreamSplitTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->char('payNo', 32)->default('')->comment('我方 唯一支付流水号');
            $table->unsignedInteger('shopId')->default(0)->comment('店铺id');
            $table->unsignedTinyInteger('sysType')->default(1)->comment('业务类型');
            $table->unsignedBigInteger('orderId')->default(0)->comment('业务id（新需求中废弃，用bizId代替）');
            $table->string('bizId', 64)->default('')->comment('业务订单号');
            $table->char('thirdPayNo', 32)->default('')->comment('三方支付流水号');
            $table->unsignedTinyInteger('payStatus')->default(0)->comment('0未支付 1支付成功');
            $table->unsignedInteger('payPrice')->default(0)->comment('支付金额，单位分');
            $table->unsignedTinyInteger('isPaidIn')->default(1)->comment('是否计入实收 0否 1是');
            $table->unsignedInteger('paySuccessTime')->default(0)->comment('支付成功时间');
            $table->unsignedInteger('payChannel')->default(0)->comment('支付渠道 1支付宝直连 2微信直连 3现金 4富友');
            $table->unsignedTinyInteger('payChannelChild')->default(0)->comment('底层支付渠道 1支付宝 2微信');
            $table->unsignedTinyInteger('payAction')->default(0)->comment('支付操作类型 1主扫 2被扫 3小程序');
            $table->unsignedInteger('fee')->default(0)->comment('手续费');
            $table->double('feeRate', 8, 6)->default(0)->comment('费率');
            $table->string('callbackUrl', 512)->default('')->comment('己方服务-支付成功回调地址');
            $table->string('subject', 32)->default('')->comment('备注');
            $table->unsignedSmallInteger('company')->default(0)->comment('公司枚举，详见rms PayConfigService');
            $table->string('companyAccount', 64)->default('')->comment('公司收款商户号');


            // 统一表字段
            $table->integer('createdAt')->default(0)->comment('创建时间');
            $table->integer('createdByUid')->default(0)->comment('创建人');
            $table->integer('updatedAt')->default(0)->comment('修改时间');
            $table->integer('updatedByUid')->default(0)->comment('修改人');

            // 索引部分
            $table->primary('payNo', 'idx_payNo');
            $table->index('orderId', 'idx_orderId');
            $table->index('thirdPayNo', 'idx_thirdPayNo');
            $table->index('createdAt', 'idx_createdAt');
            $table->index('shopId', 'shopId_idx');
            $table->index('bizId', 'idx_bizId');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '支付流水表' . $this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->create('pay_stream' . $this->name, $cFunc);
            Schema::connection("mysqlOrder_2")->create('pay_stream' . $this->name, $cFunc);

            dump($this->name);
        }

        $this->name = '';
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->dropIfExists('pay_stream' . $name);
            Schema::connection("mysqlOrder_2")->dropIfExists('pay_stream' . $name);

            dump($name);
        }

    }
}
